//
//  ProductionViewController.m
//  iStage
//
//  Created by michael on 26/07/2008.
//  Copyright 2008 Legal Education Technology Lab. All rights reserved.
//

#import "ProductionViewController.h"
#import "DataManager.h"
#import "EditTextController.h"
#import "EditDateController.h"
#import "PeopleListViewController.h"

@implementation ProductionViewController
@synthesize textEdit,dateEdit, peopleList;

-(id)initWithNibName:(NSString *)NibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
	if (self = [super initWithNibName:NibNameOrNil bundle:nibBundleOrNil]) {
		NSLog(@"Initialising Production View");
		textEdit = [[EditTextController alloc] initWithNibName:@"EditStringView" bundle:nil];
		dateEdit =[[EditDateController alloc] initWithNibName:@"EditDateController" bundle:nil];
		
		peopleList=[[PeopleListViewController alloc] initwithListType:0];
		
		self.title=@"Production Details";
		
		self.navigationItem.rightBarButtonItem = 
		[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
													  target:self action:@selector(editProductionAction)];
		self.navigationItem.leftBarButtonItem = 
		[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered 
										target:self action:@selector(backToListAction)];
		NSLog(@"Done initialising PVC");
	}
	return self;
}
/* puts thepage into edit mode*/
-(void)editProductionAction {
	self.navigationItem.leftBarButtonItem = 
	[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
												  target:self action:@selector(saveEditProductionAction)];
	self.navigationItem.rightBarButtonItem=
	[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
												  target:self action:@selector(endEditProductionAction)];
	[self setEditing:YES animated:YES];
}
/* exits edit mode*/
-(void)saveEditProductionAction {
		/* do stuff to save changes */
	NSLog(@"Saving Data to DataManager");
	[self endEditProductionAction];
}
-(void)endEditProductionAction {
	[self setEditing:NO animated:YES];
	
	self.navigationItem.rightBarButtonItem = 
	[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit 
												  target:self action:@selector(editProductionAction)];
	
	
	
	self.navigationItem.leftBarButtonItem = 
	[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered 
									target:self action:@selector(backToListAction)];
}
/* returns to the main list*/
-(void)backToListAction {
	[self.navigationController popViewControllerAnimated:YES];
}
- (id)initWithStyle:(UITableViewStyle)style {
	if (self = [super initWithStyle:style]) {
	}
	return self;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	if (section ==1) {
		return 4;
	}
	return 3;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	static NSString *MyIdentifier = @"MyIdentifier";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
	}
	switch(indexPath.section) {
		case 0:
		{
			DataManager *dm = [DataManager sharedDataManager];
			switch (indexPath.row) {
				case 0:
					//cell.text=self.ProductionName;
					cell.text=[dm.currentProduction objectForKey:@"productionname"];
					break;
				case 1:
				{
					NSDate *d = [dm.currentProduction objectForKey:@"startdate"];
					NSDateFormatter *df = [[NSDateFormatter alloc] init];
					[df setDateStyle:NSDateFormatterMediumStyle];
					cell.text= [df stringFromDate:d];
				 
				//	cell.text=[dm.currentProduction objectForKey:@"startdate"];
					[df release];
				}
					break;
				case 2:
				{
					NSDate *d = [dm.currentProduction objectForKey:@"enddate"];
					NSDateFormatter *df = [[NSDateFormatter alloc] init];
					[df setDateStyle:NSDateFormatterMediumStyle];
					cell.text= [df stringFromDate:d];
					[df release];
				}
					
					break;
				default:
					cell.text=@"Guff";;
					break;
			}
			//[dm release];
		}
			break;
		case 1:
			switch (indexPath.row) {
				case 0:
					cell.text=@"Cast List";
					break;
				case 1:
					cell.text=@"Crew List";
					break;			
				case 2:
					cell.text=@"Calendar";
					break;
				case 3:
					cell.text=@"Props List";
					break;
			}
			break;
	}	return cell;
}
-(NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section{
	if (section==0) {
		return @"Production Information";
	}
	return @"Lists";
}

- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath {
	if (indexPath.section==0) {
		return UITableViewCellAccessoryNone;
	}
	return UITableViewCellAccessoryDisclosureIndicator;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	if (indexPath.section==0) {
		//NSMutableDictionary *productionInfo =[[DataManager sharedDataManager] currentProduction];
		if (self.editing) {
			switch (indexPath.row) {
				case 0:
					
				{
					NSLog(@"Editing production name value");
					if (textEdit==nil) {
						textEdit = [[EditTextController alloc] initWithNibName:@"EditStringView" bundle:nil];
					}
					NSLog(@"Setting Value key");
					textEdit.valueName =@"productionname";
					textEdit.navigationItem.title=@"Production Name";
					textEdit.title=@"Production Name";
					[self presentModalViewController:textEdit animated:YES];
					
				}
					break;
					//}
				case 1: /*start date*/
				{
					dateEdit.ValueKey =@"startdate";
					dateEdit.navigationItem.title=@"Start Date";
					[self presentModalViewController:dateEdit animated:YES];
					
				}
					break;
				case 2:
				{
					dateEdit.ValueKey =@"enddate";
					dateEdit.navigationItem.title=@"End Date";
					[self presentModalViewController:dateEdit animated:YES];
				}
					break;
					
			}
			//[productionInfo release];
		}
		else {
			[tableView deselectRowAtIndexPath:indexPath animated:YES];

		}
	}
	if (indexPath.section==1) {
		switch (indexPath.row) {
			case 0:
			case 1:
			{
				[peopleList setListTypeTo:indexPath.row];
				NSLog([[NSString alloc] initWithFormat:@"Setting People list type %u",indexPath.row]);

				[self.navigationController pushViewController:peopleList animated:YES];
			}
				break;
			default:
				break;
		}
	}
	
}


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
	
	return UITableViewCellEditingStyleNone;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
	
	if (editingStyle == UITableViewCellEditingStyleDelete) {
	}
	if (editingStyle == UITableViewCellEditingStyleInsert) {
	}
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
	if (indexPath.section ==1) {
		return NO;
	}
	return YES;
}

/*
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/
/*
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

- (void)dealloc {
	[textEdit release];
	[dateEdit release];
	
	[super dealloc];
}


- (void)viewDidLoad {
	[super viewDidLoad];
}


- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
	[self.tableView reloadData];
}

- (void)viewDidAppear:(BOOL)animated {
	[super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
}

- (void)viewDidDisappear:(BOOL)animated {
}

- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations
	return YES;
	//	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

